<!--
 * @Author: your name
 * @Date: 2021-09-06 16:32:05
 * @LastEditTime: 2021-09-08 15:10:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\production\crausel.vue
-->
<template>
  <div class="crausela">
    <div class="btns">
      <!-- 检索框 -->
      <el-form :inline="true" size="small">
        <el-button
          @click="toAddHandler"
          type="primary"
          size="small"
          style="margin-right: 10px"
          >新增</el-button
        >
        <el-button
          type="primary"
          size="small"
          style="margin-right: 10px"
          @click="show"
          >预览</el-button>
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="content">
       <el-table :data="tableData" style="width: 100%" size="small">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="轮播图名称" prop="name"></el-table-column>
        <el-table-column label="轮播图描述" prop="introduce"></el-table-column>
        <el-table-column label="轮播图图片" prop="">
          <template v-slot="scope">
            <el-popover placement="right" width="225" trigger="hover">
              <img :src="scope.row.url" width="200" alt="图片丢失" />
              <img
                slot="reference"
                :src="scope.row.url"
                width="50"
                alt="图片丢失"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            <el-tag
              v-if="scope.row.status == '正常'"
              size="mini"
              type="success"
              >{{ scope.row.status }}</el-tag
            >
            <el-tag v-else size="mini" type="danger">
              {{
              scope.row.status
            }}
            </el-tag>
          </template>
        </el-table-column>
      
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="editHandler(scope.row)">编辑</el-button>
            <el-button type="text" style="color:red" @click="delHandler(scope.row.id)">删除</el-button>
            <el-button
              v-if="scope.row.status == '正常'"
              type="text"
              style="color: red"
              @click="offlineHandler(scope.row, $event)"
              >停用</el-button
            >
            <el-button
              v-else
              type="text"
              style="color: green"
              @click="offlineHandler(scope.row, $event)"
              >启用</el-button
            >

          </template>
        </el-table-column>
      </el-table>


          <!-- 模态框 -->
      <el-dialog  :title="title" :visible.sync="visible" @close="dialogCloseHandler">

        <el-form :model="crausel" :rules="rules" ref="saveForm">
          <el-form-item  label="图片名称"  :label-width="formLabelWidth" prop="name" >
            <el-input v-model="crausel.name"></el-input>
          </el-form-item>
          
          <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="url">
            <el-upload
              class="avatar-uploader"
              action="http://81.69.24.232/pet/jz_upload.php"
              :show-file-list="false"
              :on-success="handlePhotoSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="描述" :label-width="formLabelWidth">
            <el-input type="textarea" v-model="crausel.introduce"></el-input>
          </el-form-item>

        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="toSavehandler">确定</el-button>
        </div>

      </el-dialog>


     <!-- 模态框 -->
     <el-dialog  title="轮播图展示" :visible.sync="dialogVisible"  width="70%" >
        <div class="block">
         <el-carousel trigger="click" height="300px">
           <el-carousel-item v-for="item in tableData" :key="item">
             <img :src="item.url" width="100%" height="280px">
          </el-carousel-item>
      </el-carousel>
      </div>
       
       <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible=false">关闭</el-button>
       </div>
     </el-dialog>
   

    </div>

  </div>
    
</template>

<script>
import request from "@/utils/request";
import qs from "qs";
import _ from 'lodash'
export default {
  data(){
    return{
      tableData:[],
      title: "",
      visible: false,
      dialogVisible:false,
      crausel: {},
      formLabelWidth: "80px",
      imageUrl: null,
      rules: {
        name: [{ required: true, message: "轮播图名称不能为空", trigger: "blur" }],
        url: [{ required: true, message: "请上传轮播图图片", trigger: "blur" }],
      },

    };
  },
  methods:{
    async findAllCrausels () {
      let res = await request.get('/carousel/findAll');
      this.tableData=res.data;
    },

    //新增按钮点击事件
    toAddHandler() {
      this.crausel = {};
      this.imageUrl = null;
      this.title = "新增轮播图";
      this.visible = true;
    },

     // 图片上传成功的回调
    handlePhotoSuccess(res, file) {
      // 将返回的图片在线地址映射到数据模型中
      this.crausel.url = res;
      this.imageUrl = res;
    },

    // 模态框关闭的回调
    dialogCloseHandler() {
      this.$refs["saveForm"].resetFields(); // 置空表单验证
    },

    // 表单保存事件
    toSavehandler() {
        this.$refs["saveForm"].validate((valid) => {
          if (valid) {
          request
            .post("/carousel/saveOrUpdate", qs.stringify(this.crausel))
            .then((res) => {
              this.findAllCrausels(); // 重载数据
              this.$message.success(res.message); // 提示操作结果信息
              this.visible = false; // 关闭模态框
            });
            } else {
          return false;
        }
    });

   },
     // 编辑按钮点击事件
    editHandler(row) {
      this.crausel = _.clone(row); // 将当前行数据赋值给数据模型
      this.imageUrl = row.url;
      this.title = "编辑轮播图信息";
      this.visible = true;
    },
    //删除按钮点击事件
    delHandler(id){
        this.$confirm("是否确认该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          request.get("/carousel/deleteById", { params: { id } }).then((res) => {
           this.findAllCrausels();//重载数据
            this.$message.success(res.message); // 提示信息
          });
      });
    },

    //展示轮播图
    show(){
        this.dialogVisible = true;
        this.title = "新增轮播图";

    },

    
    //停用、启用执行处理程序
    offlineHandler(row, e) {
      this.$confirm("是否确认该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (e.target.innerText == "停用") {
          row.status=='停用'
          // this.status=row.status;
          e.target.innerText == "启用";
         
          this.findAllCrausels();
           this.$message.success(res.message); // 提示信息
        } else {
          row.status == '正常'
          // this.status=row.status;
          e.target.innerText == "停用"
          // this.findAllCrausels();
          
           this.findAllCrausels();
            this.$message.success(res.message); // 提示信息
        }
      });
    },
  },

  created(){
    this.findAllCrausels();
  }

}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>